<?php
	define( 'APP_PATH', __DIR__ );
//	error_reporting( 0 );
	date_default_timezone_set( 'Europe/Berlin' );

	require_once( APP_PATH . '/../configuration.php' );
	require_once( APP_PATH . '/db.cls.php' );

	$GLOBALS['SETTINGS']	= new JConfig();
	formatRequest();
	chkParam();

	$s_clsName				= isset( $_REQUEST['c'] ) ? $_REQUEST['c'] : 'coupons';
	$s_clsFile				= APP_PATH . "/{$s_clsName}.cls.php";
	$s_method				= isset( $_REQUEST['a'] ) ? $_REQUEST['a'] : 'index';
	
	if( !file_exists( $s_clsFile ) )
		jsonRst( null, false, "file not exists {$s_clsFile}" );

	require_once( $s_clsFile );
	if( !class_exists( $s_clsName ) )
		jsonRst( null, false, "class not exists {$s_clsName}" );

	$o_cls					= new $s_clsName;
	if( !method_exists( $o_cls, $s_method ) )
		jsonRst( null, false, "method not exists {$s_method}" );

	$v_result				= $o_cls->$s_method();
	if( $v_result === false )
		jsonRst( null, false, "method not allow {$s_method}" );
	
	jsonRst( $v_result );

	
	function trimArray( $a_para, $s_char = null, $b_htmlConversion = true ){
		if( empty( $a_para ) || !is_array( $a_para ) )
			return array();
		
		$_b_phpMagicQuotes		= ini_get( 'magic_quotes_gpc' );			//获取PHP参数转义开启状态
		
		foreach( $a_para as $_v_key => $_v_value ){
			if( is_array( $_v_value ) )
				$a_para[$_v_key]		= trimArray( $_v_value, $s_char, $b_htmlConversion );
			else{
				$a_para[$_v_key]		= @trim( $_v_value, $s_char );
				if( $b_htmlConversion )
					$a_para[$_v_key]	= htmlspecialchars( $a_para[$_v_key] );
				if( !$_b_phpMagicQuotes )
					$a_para[$_v_key]	= addslashes( $a_para[$_v_key] );
			}
		}
			
		return $a_para;
	}

	function formatRequest(){
		$_GET				= trimArray( $_GET );
		$_POST				= trimArray( $_POST );
		$_REQUEST			= array_merge( $_GET, $_POST );
		$_REQUEST['method']	= strtoupper( $_SERVER['REQUEST_METHOD'] );
		return true;
	}

	function chkParam(){
	}

	function jsonRst( $a_data, $b_rst = true, $s_msg = null ){
		$_a_result			= array( 
			'rst'	=> $b_rst, 
			'msg'	=> $s_msg,
			'data'	=> $a_data,
		);
		echo json_encode( $_a_result );
		exit;
	}
?>